<template>
	<view class="setpass">
		<HeaderBar :title="title" showBack backgroundColor="#F6F6F6" fixed />

		<view class="contentBox">
			<view class="form-item" v-if="type==1">
				<view class="title f28">钱包名称</view>
				<view class="iptStyle pass-ipt">
					<input class="input" v-model="name" name="input" placeholder="钱包名称"
						placeholder-style="color: #B2B2B2;font-size:24upx"  maxlength="10"/>
				</view>
			</view>
			<view class="form-item" v-if="type==2">
				<view class="title f28">密码提示</view>
				<view class="iptStyle pass-ipt">
					<input class="input" v-model="passwordTilp" name="input" placeholder="请输入钱包密码提示"
						placeholder-style="color: #B2B2B2;font-size:24upx" />
				</view>
			</view>
			<view class="form-item">
				<view class="title f28">钱包密码</view>
				<view class="iptStyle pass-ipt">
					<input class="input" password v-model="password" name="input" placeholder="请输入钱包密码"
						placeholder-style="color: #B2B2B2;font-size:24upx" />
				</view>
			</view>
			<view class="btn">
				<view @tap="confirm">
					确认
				</view>
			</view>
			<!-- </form> -->
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				password: "",
				passwordTilp: "",
				name: "",
				type: ""
			}
		},
		computed: {
			addressInfo() {
				return this.$store.getters.addressInfo
			},
			title() {
				if (this.type == 1) {
					return "修改钱包名称"
				}
				if (this.type == 2) {
					return "修改密码提示"
				}
			},
		},
		watch: {
			addressInfo: {
				immediate: true, // 立即执行一次监听器
				handler(newValue) {
					if (newValue) {
						this.name = newValue.name;
						this.passwordTilp = newValue.passwordTilp;
					}
				}
			}
		},
		onLoad(option) {
			this.type = option.type
		},
		methods: {
			error(tip) {
				uni.showToast({
					title: tip,
					position: "bottom",
					icon: "none",
					duration: 2000
				});
			},
			confirm() {
				if (!this.name && this.type == 1) {
					return this.error("请输入钱包名称")

				}
				if (!this.passwordTilp && this.type == 2) {
					return this.error("请输入密码提示")
				}
				if (!this.password) {
					return this.error("请输入钱包密码")
				}
				if (this.password != this.addressInfo.password) {
					return this.error("密码有误")
				}
				if (this.type == 1) {
					this.$store.dispatch('updateName', this.name)
						.then(() => {
							this.error("修改成功")
							this.$updateWalletInfo()
							uni.navigateBack()
						})
				}
				if (this.type == 2) {
					this.$store.dispatch('updatePassTilp', this.passwordTilp)
						.then(() => {
							this.error("修改成功")
							uni.navigateBack()
							this.$updateWalletInfo()
						})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.setpass {
		width: 100%;
		height: 100vh;
		background: #F6F6F6;
	}

	.contentBox {
		padding: 0 50upx;

		.form-item {
			margin-top: 30upx;

			.title {
				font-size: 30upx;
				color: #3D3D3D;
			}

			.f28 {
				font-size: 28upx;
			}

			.iptStyle {
				border-radius: 20upx;
				background-color: #FFFFFF;

				.input {
					padding: 26upx 24upx;
					font-size: 26upx;
				}
			}

			.name-ipt {
				margin-top: 26upx;
			}

			.pass-ipt {
				margin-top: 26upx;
			}

			.confirm-pass-ipt {
				margin-top: 12upx;
			}

		}



		.mt {
			margin-top: 900upx;
		}

		.btn {
			font-size: 26upx;
			color: #F6F6F6;
			line-height: 88upx;
			background: linear-gradient(180deg, #A9E993 0%, #368321 100%);
			border-radius: 10upx;
			text-align: center;
			margin-top: 100upx;
		}
	}
</style>